/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo;

import java.util.ArrayList;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Sergio
 */
public class TabelaCadInternacao extends AbstractTableModel{

    protected Vector dataVector;
    
    private ArrayList<Internacao> cadInt = new ArrayList<Internacao>();
    private String[] colunas = {"Nome", "Descricao", "IdLeito","Status-Internação"};
    
   public static final int INDICE_NOME = 0;
    public static final int INDICE_DESCRICAO = 1;
     public static final int INDICE_LEITO = 2;
    public static final int INDICE_FLAG = 3;
    
    
    
    public int getRowCount() {
        return cadInt.size();
    }

    
    public int getColumnCount() {
        return colunas.length;
    }
    
    public String getColumnName(int coluna) {
        return colunas[coluna];
    }

    
    public Object getValueAt(int rowIndex, int columnIndex) {
        String[] linha = cadInt.get(rowIndex).toArray();
        return linha[columnIndex];
    }
    public void addCadInternacao(Internacao cadInternacao) {
        if(cadInternacao.getStatus()){
            cadInt.add(cadInternacao);
        }
        
        this.fireTableDataChanged();
    }
    

    public Internacao getSelectedCadInt(int linhaSelecionada) {
        return cadInt.get(linhaSelecionada);
    }
    
    public void removeCadInt(int linhaSelecionada){
        cadInt.remove(linhaSelecionada);
        this.fireTableDataChanged();
    }
    
    public void limpaArray(){
        cadInt.clear();
    }
    
    private void justifyRows(int from, int to) {
        // Sometimes the DefaultTableModel is subclassed
        // instead of the AbstractTableModel by mistake.
        // Set the number of rows for the case when getRowCount
        // is overridden.
        dataVector.setSize(getRowCount());

        for (int i = from; i < to; i++) {
            if (dataVector.elementAt(i) == null) {
                dataVector.setElementAt(new Vector(), i);
            }
            ((Vector)dataVector.elementAt(i)).setSize(getColumnCount());
        }
    }
    
    public void setNumRows(int rowCount){
        int old = getRowCount();
        if (old == rowCount) {
            return;
        }
        dataVector.setSize(rowCount);
        if (rowCount <= old) {
            fireTableRowsDeleted(rowCount, old-1);
        }
        else {
            justifyRows(old, rowCount);
            fireTableRowsInserted(old, rowCount-1);
        }
        
        limpaArray();
    }
    
}
